package org.fas.display
{
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;

	public class SpringButton extends FSprite
	{
		public var prototype:MovieClip;
		private var isBack:Boolean = false;
		public function SpringButton(_prototypeButton:MovieClip = null,_x:Number =0,_y:Number=0,_container:DisplayObjectContainer=null)
		{
			super(_x,_y,_container);
			this.buttonMode = true;
			if(_prototypeButton!=null){
				this.bind(_prototypeButton);
			}
			this.addEventListener(MouseEvent.MOUSE_OVER,evtMouseOver);
			this.addEventListener(MouseEvent.MOUSE_OUT,evtMouseOut);
		}
		public function bind(_prototypeButton:MovieClip=null):void{
			this.unBind();
			if(_prototypeButton==null){
				
			}else{
				this.prototype = _prototypeButton;
				this.addChildAt(prototype,this.numChildren);
				prototype.addFrameScript(this.prototype.totalFrames-1,prototype.stop);
				prototype.gotoAndStop(1);
			}
		}
		public function unBind():void{
			if(this.prototype){
				this.removeChild(this.prototype);
			}
		}
		private function evtMouseOver(_e:MouseEvent):void{
			this.removeEventListener(Event.ENTER_FRAME,evtEnterFrame);
			if(this.prototype!=null){
				prototype.gotoAndPlay(Math.min(this.prototype.currentFrame+1,this.prototype.totalFrames));
			}
		}
		private function evtMouseOut(_e:MouseEvent):void{
			this.addEventListener(Event.ENTER_FRAME,evtEnterFrame);
		}
		private function evtEnterFrame(_e:Event):void{
			if(this.prototype!=null){
				if(this.prototype.currentFrame==1){
					this.removeEventListener(Event.ENTER_FRAME,evtEnterFrame);
				}else{
					prototype.gotoAndStop(prototype.currentFrame-1);
				}
			}
		}
		override public function destruct():void{
			super.destruct();
			this.removeEventListener(MouseEvent.MOUSE_OVER,evtMouseOver);
			this.removeEventListener(MouseEvent.MOUSE_OUT,evtMouseOut);
		}
	}
}